<?php
abstract class PaymentAPI extends Payment{
	
	protected $_APIObj;
	protected $_apiName;
	protected $_paymentData;
	protected $_response;
	protected $_request;
	
	abstract function apiInit();
	abstract function apiRequest($request);
	abstract function apiResponse($response);	
	abstract function apiPaymentVaild();
	
	public function __construct($paymetnData = null){
		if ($paymetnData instanceof PaymentInfo){
			$this->_paymentData = $paymetnData;
			$this->setPaymentCode($paymetnData->getPaymentCode());
		}
		
		
		$this->apiInit();
	}
	
	public function setRequest($reqeust){
		$this->_request = $reqeust;
		return $this;
	}
	
	public function getRequest(){
		return $this->_request;	
	}
	
	public function setResponse($response){
		$this->_response = 	$response;
		return $this;
	}
	
	public function getResponse(){
		return $this->_response;
	}
	
	public function setAPIobj($apiObj){
		$this->_APIObj = $apiObj;
		return $this;
	}
	
	public function getAPIobj(){
		return $this->_APIObj;
	}
	
	protected function setApiName($apiName){
		$this->_apiName = $apiName;
		return $this;
	}
	
	protected function getApiName(){
		return $this->_apiName;
	}
	
// 	public function paymentLog($logs,$tpye = 'info'){
// 		Yii::app()->fileLog
// 			->setNamespace('payment')
// 			->addDir(date('Y'))
// 			->addDir(date('m'))
// 			->addDir(date('d'))
// 			->addDir($this->getApiName())
// 			->addDir($this->getPaymentCode())
// 			->setFile($this->getApiName().'_'.$this->getPaymentCode())
// 			->addLog($logs,$tpye);
// 		return $this;
// 	}

	public function paymentLog($logs,$tpye = 'info'){
		Yii::app()->fileLog
		->setNamespace('payment')
		->addDir(date('Y'))
		->addDir(date('m'))
		->addDir(date('d'))
		->addDir($this->getApiName())
		->addDir($this->getPaymentCode())
		->setFile($this->getApiName().'_'.$this->getPaymentCode())
		->addLog($logs,$tpye);
		$model = new PaymentLog();
		$model->type = ($this->getApiName() == 'AuthorizeNet')?2:1;
		$model->request = $this->getPaymentCode();
		$model->customer_id = Yii::app()->user->id;
		$model->created = time();
		$model->response = json_encode($logs);
		$model->save();
		return $this;
	}
		
	protected function vaild(){
		return true;
	}
	
	public function setPaymentData($paymentData){
		if ($paymentData instanceof PaymentInfo){
			$this->_paymentData = $paymentData;
		}
		return $this;
	}
	
	public function __get($name){
		
		return $this->_paymentData->$name;
	}
}